package com.ylq.todolsitserver.controller;

import com.ylq.todolsitserver.entity.Category;
import com.ylq.todolsitserver.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 创建分类
     * @param category
     * @return
     */
    @PostMapping
    public ResponseEntity<Category> createCategory(@RequestBody Category category) {
        log.info("创建分类");
        return new ResponseEntity<>(categoryService.createCategory(category), HttpStatus.CREATED);
    }

    /**
     * 获取全部分类
     * @return
     */
    @GetMapping
    public ResponseEntity<List<Category>> getAllCategories() {
        log.info("获取全部分类");
        return ResponseEntity.ok(categoryService.getAllCategories());
    }

    /**
     * 更新分类
     * @param id
     * @param category
     * @return
     */
    @PutMapping("/{id}")
    public ResponseEntity<Category> updateCategory(@PathVariable Integer id, @RequestBody Category category) {
        log.info("更新分类");
        return ResponseEntity.ok(categoryService.updateCategory(id, category));
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCategory(@PathVariable Integer id) {
        log.info("删除分类");
        categoryService.deleteCategory(id);
        return ResponseEntity.noContent().build();
    }
}